from catma import enum
GROUPS = enum.Enum('system', 'Generic', 'Physics', 'Content', 'Combat', 'Logic', 'Misc', value_type=lambda self, i, k: k)
BUILTIN_GROUP = GROUPS.system
DEFAULT_GROUP = GROUPS.Generic
CUSTOM_FLAGS = enum.Enum('DUMMY', value_type=lambda self, i, k: (1 << i))
DEFAULT_CUSTOM_FLAGS = 0
DEFAULT_CUSTOM_FLAGS_FOR_CLASS_TYPE = 0
DEPENDENT_MODULES = ['dustAttribs2', 'dustContent']

def EntryPoint(*args, **kwargs):
    import dustAttribs2
    dustAttribs2.PopulateAll(*args, **kwargs)


import os
theDB = None

def GetCatmaDB():
    global theDB
    if not theDB:
        from sake.app import app
        from sake.const import APP_ROLE_CLIENT, APP_ROLE_SERVER
        if app:
            if app.HasRole(APP_ROLE_CLIENT):
                vault = app.GetService('vaultClient')
            elif app.HasRole(APP_ROLE_SERVER):
                vault = app.GetService('vaultServer')
            location = vault.GetFilePath('catmaDB.pickle')
        else:
            attempts = 100
            relative = ''
            while not os.path.exists(os.path.join(relative, 'eve\\common\\common.ini')):
                attempts -= 1
                relative += '..\\'
                if (attempts == 0):
                    raise RuntimeError("ArchiveBuild: Couldn't find branch root.")

            location = os.path.join(relative, 'eve/dust/ue3/DustGame/vault/catmaDB.pickle')
        if ((location is None) or (not os.path.exists(location))):
            raise RuntimeError("Can't find catmaDB.pickle in vault folder\n   Possible fixes:\n   1. Run start_vaultgen.bat from eve/server folder. It will auto-generate all cache files.\n   2. Grab the cache files from the latest build and copy it to your vault folder.\n      (i.e. from here \\\\bamboo\\builds\\CN_MAIN\\CN_MAIN_232240_00006\\PC\\ue3\\DustGame\\vault\n   3. Ask someone around you for assistance. Show this error message")
        import catma.catmaDB
        theDB = catma.catmaDB.LoadData(open(location, 'rb'))
        print 'CATMA DB loaded:'
        print theDB
    return theDB



def SetCatmaDB(newCatmaDB):
    global theDB
    theDB = newCatmaDB



